package com.glela.live.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * Created by jinweiwei on 2018/4/2.
 */
public class RandomCharUtil {

    static char[] A_Z = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'
            ,'S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m'
            ,'n','o','p','q','r','s','t','u','v','w','x','y','z'};

    public static String getRandomChar() throws UnsupportedEncodingException {
        Random random=new Random();

        String ctmp="";

        String rbase[]={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};

        //生成第一位区码

        int r1=random.nextInt(3)+11;//生成11-14随机数

        String str_r1=rbase[r1];

        //生成第二位区码

        int r2;

        if(r1==13){

            r2=random.nextInt(10);

        }

        else{

            r2=random.nextInt(16);

        }

        String str_r2=rbase[r2];

        //生成第一位位码

        int r3=random.nextInt(6)+10;

        String str_r3=rbase[r3];

        //生成第二位位码

        int r4;

        if(r3==10){

            r4=random.nextInt(14)+2;

        }else{

            r4=random.nextInt(16);

        }

        String str_r4=rbase[r4];

        //将生成的区码放入第一个元素，位码放入第二个元素

        byte a[]=new byte[2];

        a[0]=(byte)Integer.parseInt(str_r1+str_r2,16);//转换成16进制

        a[1]=(byte)Integer.parseInt(str_r3+str_r4,16);

        ctmp=new String(a,"gb2312");
        return ctmp;
    }

    public static String randomNickName(int i) throws UnsupportedEncodingException {
        if (0==i){
            StringBuilder sb = new StringBuilder();
            int k = (int)(Math.random()*5)+1;
            for (int j = 0; j < k; j++) {
                sb.append(getRandomChar());
            }
            return sb.toString();
        }
        if (1==i){
            Random random=new Random();
            StringBuilder sb = new StringBuilder();
            int k = (int)(Math.random()*5)+1;
            for (int j = 0; j < k; j++) {
                sb.append(getRandomChar());
            }
            return sb.append(random.nextInt(1000)).toString();
        }
        Random random=new Random();
        StringBuilder sb = new StringBuilder();
        int k = (int)(Math.random()*5)+1;
        for (int j = 0; j < k; j++) {
            sb.append(getRandomChar());
        }
        int az = (int)(Math.random()*5)+1;
        for (int j = 0; j < k; j++) {
            sb.append(A_Z[random.nextInt(52)]);
        }
        return sb.toString();
    }
}
